/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import com.rh.util.Parser;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hayden
 */
public class GameEffectsBuffer extends AbstractXmlBuffer {
    private static void debug(Object o)  {
        // System.out.println("GameEffectsBuffer: " + o);
    }
    
    public static enum Mode {
        LOCATION;
    }
    
    private Mode mode = null;
    
    private List<GameEffectKeys> initializations = new ArrayList<GameEffectKeys>();
    public List<GameEffectKeys> getInitializations()  {
        return initializations;
    }
    
    private LocationBuffer locBuffer = new LocationBuffer();
    private String typeID = "";
    private String objectID = "";
    private String whomID = "";
    private String whomID2 = "";

    public GameEffectsBuffer()  {
        super();
        init();
    }
    
    void init()  {
        locBuffer = new LocationBuffer();
        typeID = "";
        objectID = "";
        whomID = "";        
        whomID2 = "";        
    }
    
    void characters(String pValue) {
        if (mode == Mode.LOCATION)  {
            locBuffer.characters(pValue);
        }
        
        pValue = Parser.trim(pValue);
        if (pValue.length() == 0)  return;
        
        if ("Type".equals(getStartElement()))  {
            typeID = pValue;
        }
        else if ("Object".equals(getStartElement()))  {
            objectID = pValue;
        }
        else if ("Whom".equals(getStartElement()))  {
            whomID = pValue;
        }
        else if ("Whom2".equals(getStartElement()))  {
            whomID2 = pValue;
        }
    }

    @Override
    void startElement(String pName)  {
        super.startElement(pName);
        if ("location".equals(pName))  {
            mode = Mode.LOCATION;
        }
        
        if (mode == Mode.LOCATION)  {
            locBuffer.startElement(pName);
        }
    }

    @Override
    void endElement(String pName)  {
        if ("gameEffect".equals(pName))  {
            debug("***" + typeID);
            GameEffectKeys gek = new GameEffectKeys();
            gek.setLocation(locBuffer.getLocation());
            gek.setObjectID(objectID);
            gek.setType(typeID);
            gek.setWhomID(whomID);
            gek.setWhomID2(whomID2);
            initializations.add(gek);
            init();
        }
        else if ("location".equals(pName))  {
            mode = null;
        }
        
        if (mode == Mode.LOCATION)  {
            locBuffer.endElement(pName);
        }
    }    
    
}
